<?php

$root = dirname(__FILE__);
define('BASEPATH', $root.'/');
require_once $root.'/function.php';
require_once $root.'/config.php';
require_once $root.'/model.php';


$model = new Model();

//獲取中轉站的server信息
$baseServer = $model->getBaseServer();
initBaseServerInfo($baseServer);

if( !file_exists($baseServerInfo['fixCoverDir'].'/ok.txt')){
   sleep(60);
   die("\n ===== sleep 60s ======\n");
}
//遍历上传影片的目录
$dateDirList = myScandir($baseServerInfo['fixCoverDir'], array('jpg'));
//var_dump($dateDirList);exit;
if( !is_array($dateDirList['file'])){
   die("\n==== The fixCoverDir is Empty Or is not Exists!=====\n");
}

foreach($dateDirList['file'] as $val){
   $path_info = pathinfo($val);
   $avkey = substr($path_info['filename'], 0, -2);
   if(!$avkey){
      @unlink($val);
      continue;
   }
   $data = $model->getAvByid($avkey,null,null,1);
   if(!$data){
      @unlink($val);
      continue;
   } 
   $storageServer = $model->getStorageServerBySid($data['serverid']);
   if( !isset($storageServer['serverid'])){
      die("\n=====The storageServer serverid is Empty!====\n");
   }
   rsyncFile($val); 
   
}

@unlink($baseServerInfo['fixCoverDir'].'/ok.txt');
echo "\n =========== 修復Video 封面圖片結束! =============== \n";
sleep(60);
